package com.lxn.handler;

import com.alibaba.fastjson.JSON;
import com.lxn.feignclients.FastdfsFeignClient;
import com.lxn.hrm.constants.MQConstants;
import com.lxn.hrm.util.myFileUtils;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class PageStaticHandler {
    @Autowired
    private FastdfsFeignClient fastdfsFeignClient;
    //监听叶明静态化队列
    @RabbitListener(queues = {MQConstants.NAME_QUEUE_PAGE_DIRECT})
    public void receive_email(String msg, Message message, Channel channel) throws IOException {

        Map<String,String> map = JSON.parseObject(msg,Map.class);
        String htmlPathFastdfs = map.get("htmlPathFastdfs");
        String physicalPath = map.get("physicalPath");
        //下载
        byte[] htmlBytes = fastdfsFeignClient.download(htmlPathFastdfs);
        if(htmlBytes!=null&&htmlBytes.length>0){
            File file = myFileUtils.createFile(physicalPath);

            FileCopyUtils.copy(htmlBytes,file);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }
}
